package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.Group;
import com.suyang.syt.model.User;
import com.suyang.syt.model.UserGroup;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.GroupDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.UserGroupDTO;
import com.suyang.syt.service.interfaces.GroupService;
import com.suyang.syt.service.interfaces.UserGroupService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.StringUtil;

/** 用户组管理 */
@Controller
@RequestMapping("/admin/group")
@AuthClass("login")
public class AdminGroupController {
	@Autowired
	private GroupService groupService;
	@Autowired
	private UserGroupService userGroupService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "group/list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<Group> list(HttpServletRequest request, int page, int rows, String sort, String order, String name) {
		try {
			return groupService.listByEasyUI(page, rows, sort, order, name);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/findAll", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<Group> findAll(HttpServletRequest request, int page, int rows, String sort, String order) {
		try {
			return groupService.listByEasyUI(page, rows, sort, order, null);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, Group group) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				group.setCreateName(user.getUsername());
				group.setCreateTime(DateFormat.dateToString(new Date()));
			}
			return groupService.add(group);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj delete(HttpServletRequest request, String ids) {
		try {
			return groupService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, GroupDTO groupDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				groupDTO.setModifyName(user.getUsername());
				groupDTO.setModifyTime(DateFormat.dateToString(new Date()));
			}
			return groupService.update(groupDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@AuthMethod(role = "ROLE_PXJG")
	@RequestMapping(value = "/clearUser", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteUser(HttpServletRequest request, String groupIds) {
		try {
			return groupService.deleteClearUser(groupIds);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/listGroup", method = RequestMethod.POST)
	@ResponseBody
	public List<Group> listGroupUser(HttpServletRequest request) {
		try {
			return groupService.listGroup();
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/user/list", method = RequestMethod.GET)
	public String listGroupUser(String gid, Model model) {
		if (EmptyUtil.isNotEmpty("gid")) {
			model.addAttribute("gid", gid);
		}
		return "group/user/list";
	}

	@RequestMapping(value = "/user/list", method = RequestMethod.POST)
	@ResponseBody
	public PagerEasyUI<UserGroupDTO> listGroupUser(HttpServletRequest request, String gid, int page, int rows, String sort, String order) {
		try {
			long groupId = Long.parseLong(gid);
			return userGroupService.listByEasyUI(2, groupId, page, rows, sort, order);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/user/add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj addUserGroup(HttpServletRequest request, UserGroup userGroup) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			if (user != null) {
				userGroup.setCreateName(user.getUsername());
				userGroup.setCreateTime(DateFormat.dateToString(new Date()));
			}
			return userGroupService.add(userGroup);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/user/delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxObj deleteUserGroup(HttpServletRequest request, String ids) {
		try {
			return userGroupService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
